home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / pcpilot.arc / SCR.ARC / SCRPUTW.ASM < prev    next >
Assembly Source File  |  1990-01-14  |  4KB  |  144 lines

  1. ;
  2. ; SCRPUTW.ASM
  3. ;
  4. ; From the book "Systems Programming in Turbo C", by Michael J. Young
  5. ;
  6. ; This version of ScrPutWin() is to be used for CGA monitors
  7. ; that produce 'snow' when writing directly to the screen
  8. ;
  9.  
  10.         EXTRN _VideoSeg:word
  11.  
  12.         assume    cs:_text
  13.  
  14. _text        segment byte public 'code'
  15.  
  16.  
  17. ; ------------------------- ScrPutWin() ---------------------------
  18. public        _ScrPutWin
  19.  
  20. _ScrPutWin    proc near
  21.  
  22. sframe        struc
  23. BasePtr        dw        ?                ; Template to access stack frame
  24. RetAd        dw        ?                ; Position of saved BP register
  25. buf_ad        dw        ?                 ; other models should use 'dd'
  26. ulr_s        dw        ?                ; Upper-left row source
  27. ulc_s        dw        ?                ; Upper-left column source
  28. lrr_s        dw        ?                ; Lower-right row source
  29. lrc_s        dw        ?                ; Lower-right column source
  30. ulr_d        dw        ?                ; Upper-left row destination
  31. ulc_d        dw        ?                ; Upper-left column destination
  32. sframe        ends
  33.  
  34. frame        equ        [bp - BasePtr]    ; Base for accessing stack frame
  35.                                     ; Standard initialization
  36.             push    bp
  37.             mov        bp, sp
  38.             sub        sp, BasePtr
  39.             push    di
  40.             push    si
  41.  
  42.             mov        si, frame.buf_ad
  43.  
  44.             mov        ax, frame.ulr_s        ; Multiply starting row by 160
  45.             mov        bx, ax
  46.             mov        cl, 7
  47.             shl        ax, cl
  48.             mov        cl, 5
  49.             shl        bx, cl
  50.             add        bx, ax
  51.  
  52.             mov        ax, frame.ulc_s        ; Multiply starting column by 2
  53.             shl        ax, 1
  54.             add        bx, ax            ; Total offset in BX
  55.             add        si, bx            ; Add offset to SI
  56.                                     ; SI now contains offset in source buffer
  57.             mov        ax, _VideoSeg        ; Set ES to CGA video buffer
  58.             mov        es, ax
  59.  
  60.                                     ; Calculate video memory offset
  61.                                     ;    = (row * 160) + (col * 2)
  62.             mov        di, frame.ulr_d    ; Place row in DI & multiply by 160
  63.             mov        ax, di
  64.             mov        cl, 7
  65.             shl        di, cl
  66.             mov        cl, 5
  67.             shl        ax, cl
  68.             add        di, ax
  69.  
  70.             mov        ax, frame.ulc_d    ; Multiply col by 2
  71.             shl        ax, 1
  72.             add        di, ax            ; Add (col * 2) to DI
  73.                                     ; DI now contains video memory offset
  74.  
  75.                                     ; Calculate number of rows in BL
  76.             mov        bl, byte ptr frame.lrr_s
  77.             sub        bl, byte ptr frame.ulr_s
  78.             inc        bl
  79.                                     ; Calculate # of bytes/row in BH
  80.             mov        bh, byte ptr frame.lrc_s
  81.             sub        bh, byte ptr frame.ulc_s
  82.             inc        bh
  83.             shl        bh, 1
  84.  
  85.             mov        ah, 160            ; Calculate SI, DI increment in AH
  86.             sub        ah, bh
  87.  
  88.             mov        dx, 3dah        ; Keep crt status register in DX
  89.  
  90.             cld
  91.             mov        ch, 0            ; Only need LSB of CX
  92.  
  93. b01:        mov        cl, bh            ; Load bytes/row into CX
  94.  
  95.                                     ; Pause until the beginning of a
  96.                                     ; vertical retrace
  97. b02:        in        al, dx            ; Loop to complete any retrace period
  98.             test    al, 8            ; in progress
  99.             jnz        b02
  100. b03:        in        al, dx            ; Loop until start of a new vertical
  101.             test    al, 8            ; retrace
  102.             jz        b03
  103.  
  104.             rep        movsb            ; Move entire row during vertical retrace
  105.             mov        cl, ah            ; AH stores SI, DI increment value
  106.             add        si, cx            ; Adjust SI, DI to start of next row
  107.             add        di, cx
  108.             dec        bl                ; Decrement row counter
  109.             jz        b06                ; Quit if no more rows
  110.  
  111.                                     ; Move another row, byte by byte, while
  112.                                     ; waiting for next vertical retrace.
  113.                                     ; Pause until the beginning of a
  114.                                     ; horizontal retrace.
  115.             mov        cl, bh            ; Loop to complete any retrace period
  116. b04:        in        al, dx            ; in progress
  117.             test    al, 1
  118.             jnz        b04
  119.             cli                        ; Don't want interrupt now
  120. b05:        in        al, dx            ; Loop until start of a new horizontal
  121.             test    al, 1            ; retrace
  122.             jz        b05
  123.             movsb                    ; Move one byte
  124.             sti                        ; Byte transferred, so interrupts ok
  125.             loop    b04                ; Loop until entire row is moved
  126.             mov        cl, ah
  127.             add        si, cx            ; Adjust SI, DI to start of next row
  128.             add        di, cx
  129.             dec        bl                ; Decrement row counter
  130.             jz        b06                ; Quit if no more rows
  131.             jmp        b01                ; Go back to move another row during the
  132.                                     ; next vertical retrace
  133.  
  134. b06:        pop        si                ; Restore C register variables
  135.             pop        di
  136.             mov        sp, bp
  137.             pop        bp
  138.             ret                        ; Return to C program
  139.  
  140. _ScrPutWin    endp
  141.  
  142. _text        ends
  143.             end
  144.